

const { getAllLaunches } = require('../../models/launches.model');

function httpGetAllLaunches(req, res) {
    return res.status(200).json(getAllLaunches());
}


function httpAddNewLaunch(req, res) {
    const launch = req.body;
    if (!launch.mission || !launch.rocket || !launch.launchDate
         || !launch.target) {
         return res.status(400).json({
         error: '缺少必要的发射属性',
         });
         }
        
   

   launch.launchDate = new Date(launch.launchDate);
   if (isNaN(launch.launchDate)) {

     return res.status(400).json({
     error: '发射⽇期⽆效',
     });
     }
     addNewLaunch(launch);
     return res.status(201).json(launch);
    }
    
    function httpAbortLaunch(req, res) {
        const launchId = Number(req.params.id);
        if (!existsLaunchWithId(launchId)) {
        return res.status(404).json({
        error: '发射任务不存在',
        });
        }
        const aborted = abortLaunchById(launchId);
        return res.status(200).json(aborted);
       }
       


module.exports = {
    httpGetAllLaunches,
    httpAddNewLaunch,
    httpAbortLaunch,
}